package cn.chia.front.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.chia.front.dao.IItemDAO;
import cn.chia.pojo.TblActivityType;
import cn.chia.pojo.TblCatagorgy;
import cn.chia.pojo.TblItems;

@Component
public class ItemDAOImpl extends HibernateDaoSupport implements IItemDAO{

	@Autowired
	public ItemDAOImpl(HibernateTemplate hibernateTemplate){
		super.setHibernateTemplate(hibernateTemplate);
	}
	
	/**
	 * @author xyh
	 * @param pageNo 当前页数
	 * @param pageSize 每页的记录数量
	 * @param keyword 用户提供搜索的关键字
	 * @param column 用户选择的查询字段，比如名称，种类等。
	 * @Todo  query all items(查询全部商品)
	 * @return conllection of all items （返回全部商品的list集合） 
	 * @throws Exception
	 */
	public List<?> findAll(int pageNo, int pageSize, String keyword,
			String column) throws Exception {
			// 通过一个动态的column,设置要按照哪个属性来查询
			String hql = "FROM TblItems AS p WHERE p." + column + " LIKE ?";
			Query query = getSession().createQuery(hql);
			// 设置参数
			query.setString(0, "%" + keyword + "%");
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
		return query.list();
	}


	/**
	 * @author xyh
	 * @param keyword 用户提供搜索的关键字
	 * @param column 用户选择的查询字段，比如名称，种类等。
	 * @Todo obtain Total record number,In order to used the page(获取全部记录数，为了分页)
	 * @return int (the number of pages)（返回一个int类型的记录数）
	 * @throws Exception
	 */
	public int getAllCount(String keyword, String column) throws Exception {
		
		// 查询的不是pojo对象时,需要编写SELECT语句
		String hql = "SELECT COUNT(p) FROM TblItems AS p WHERE p." + column + " LIKE ?";
		Query query = getSession().createQuery(hql);
		query.setString(0, "%" + keyword + "%");
		return ((Long) query.uniqueResult()).intValue();
	}

	

	/**
	 * @author xyh
	 * @param catagorgyId 商品的种类id
	 * @Todo 按照种类id来查询商品的某一条记录。
	 * @return 商品的某一条记录。
	 * @throws Exception
	 */
	public List<TblItems> findByCatagorgy(Integer catagorgyId) throws Exception {
		String hql = "FROM TblItems AS t WHERE t.tblCatagorgy.catagorgyId ="+catagorgyId+"";
		Query query = getSession().createQuery(hql);
		
		return query.list();	
	}

	/**
	 * @author Administrator
	 * @param activityTypeId
	 * @Todo 按照活动种类查询商品
	 * @return 按照活动种类查询出来的商品list集合
	 * @throws Exception
	 */
	public List<TblItems> findByActivity(Integer activityTypeId) throws Exception{
		String hql = "FROM TblItems AS t WHERE t.tblActivity.tblActivityType.typeId="+activityTypeId+"";
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	

	/**
	 * @Todo 修改商品的活动情况
	 * @param itemId
	 * @throws Exception
	 */
	public void updateItemsActivity(String itemId,Integer activityId) throws Exception {
		String hql = "update TblItems as t set t.tblActivity.activityId ="+activityId+" WHERE t.itemId = '"+itemId+"'";
		Query query = getSession().createQuery(hql);
		query.executeUpdate();
	}

	//根据ID找商品
	public TblItems findById(String id) throws Exception {
		Session session = this.getSession();
		TblItems items = (TblItems) session.createQuery("from TblItems where itemId="+id+"").list().get(0);
		return items;
	}
	
	//根据商品名称查找商品
	public List<TblItems> findByName(String itemName) throws Exception {
		String hql = "FROM TblItems AS p WHERE p.itemName LIKE ?";
		Query query = getSession().createQuery(hql);
		query.setString(0, "%" + itemName + "%");
		return query.list();
	}

	public String findCatagorgyName(Integer catagorgyId) throws Exception {
		String hql = "FROM TblCatagorgy AS t WHERE t.catagorgyId = "+catagorgyId+"";
		Query query = getSession().createQuery(hql);
		TblCatagorgy tblCatagorgy = (TblCatagorgy) query.list().get(0);
		String name = tblCatagorgy.getCatagorgyName();
		return name;
	}


}
